import { CommonIdType } from '@/model/id-or-enum.model';
import { Message } from '@/model/msg.model';
import { Artist, MusicInfo, MVInfo } from '@/model/music-info.model';
import {
  AlbumResponse,
  ArtistSummaryResponse,
  ArtistsAlbumResponse,
  TopAlbumResponse,
  ArtistDescResponse,
  ArtistMVResponse,
  CommentTypeResponse,
  SongListInfoResponse,
  PlayListResponse,
  TopListResponse,
  PlayListDetailResponse,
  RecommendSongsResponse,
  LyricResponse,
  HotMVResponse,
  MVDetailResponse,
  SearchDefaultResponse,
  SearchHotResponse,
  TopPlayListResponse,
  MVUrlResponse,
  LoginResponse,
  QRKeyResponse,
  QRCreateResponse,
  QRCheckResponse,
} from '@/model/response-type.model';
import {
  SearchResponseType,
  SearchType,
  SearchTypeMap,
} from '@/model/search-response.model';
import { TopListInfo } from '@/model/top-list.model';
import axios from '@/utils/http';
import * as _ from 'lodash';

// api版本4.0.8 NeteaseCloudMusicApi
export type CommentItemType = 'music' | 'playList' | 'album' | 'mv';

export class APIType {
  /**
   * 获取歌曲信息，主要取里面的url
   */
  async getMusicUrl(id: CommonIdType | CommonIdType[]): Promise<MusicInfo[]> {
    if (_.isArray(id)) {
      id = id.join(',');
    }
    const result = await axios.get<any>(`/song/url?id=${id}`);
    return result?.data;
  }
  /**
   * 获取专辑详情
   */
  async getAlbumDetail(id: CommonIdType): Promise<AlbumResponse> {
    const result = await axios.get<any>(`/album?id=${id}`);
    return result;
  }
  /**
   * 获取专辑热榜列表
   */
  async getTopAlbumList(
    offset: number,
    limit = 30,
    year?: number,
    month?: number
  ): Promise<TopAlbumResponse> {
    const params = {
      offset,
      limit,
    };
    if (year != null) {
      _.set(params, 'year', year);
    }
    if (month != null) {
      _.set(params, 'month', month);
    }
    const result = await axios.get<any>('/top/album', params);
    return result;
  }
  /**
   * 获取歌手的专辑
   */
  async getArtistAlbum(
    id: CommonIdType,
    offset: number,
    limit = 30
  ): Promise<ArtistsAlbumResponse> {
    const result = await axios.get<any>(
      `/artist/album?id=${id}&limit=${limit}&offset=${offset}`
    );
    return result?.data;
  }
  /**
   * 获取歌手的部分信息、如热门歌曲、歌手信息
   */
  async getArtistsSummary(id: CommonIdType): Promise<ArtistSummaryResponse> {
    const result = await axios.get<any>(`/artists/?id=${id}`);
    return result;
  }
  /**
   * 获取歌手的介绍信息
   */
  async getArtistDesc(id: CommonIdType): Promise<ArtistDescResponse> {
    const result = await axios.get<any>(`/artist/desc?id=${id}`);
    return result;
  }
  /**
   * 获取歌手的MV信息
   */
  async getArtistMV(id: CommonIdType): Promise<ArtistMVResponse> {
    const result = await axios.get<any>(`/artist/mv?id=${id}`);
    return result;
  }
  /**
   * 获取相似歌手
   */
  async getSimiArtist(id: CommonIdType): Promise<Artist[]> {
    let result: { artists: Artist[] };
    result = await axios.get(`/simi/artist?id=${id}`);
    return result?.artists;
  }
  /**
   * 获取歌曲详情
   */
  async getSongDetail(id: CommonIdType | CommonIdType[]): Promise<MusicInfo[]> {
    if (_.isArray(id)) {
      id = id.join(',');
    }
    const result = await axios.get<any>(`/song/detail?ids=${id}`);
    return result?.songs;
  }
  /**
   * 获取评论
   */
  async getCommentByType(
    type: CommentItemType,
    id: CommonIdType,
    offset: number,
    limit = 10
  ): Promise<CommentTypeResponse> {
    const result = await axios.get<any>(
      `/comment/${type}?id=${id}&limit=${limit}&offset=${offset}`
    );
    return result;
  }
  /**
   * 获取推荐歌单
   */
  async getPersonalized(limit = 30): Promise<SongListInfoResponse> {
    const result = await axios.get<any>('/personalized', {
      limit,
    });
    return result;
  }
  /**
   * 获取个性化推荐MV
   */
  async getPersonalizedMV(): Promise<MVInfo[]> {
    const result = await axios.get<any>('/personalized/mv');
    // code , type, result;
    return result?.result;
  }
  /**
   * 获取歌单
   */
  async getPlayList(
    offset = 0,
    limit = 10,
    order = 'hot'
  ): Promise<TopPlayListResponse> {
    const result = await axios.get<any>('/top/playlist', {
      limit,
      order,
      offset,
    });
    return result;
  }
  /**
   * 获取榜单类型
   */
  async getTopList(): Promise<TopListResponse> {
    const result = await axios.get<any>('/toplist');
    return result;
  }
  /**
   * 获取榜单详情
   */
  async getTopListDetail(id: CommonIdType): Promise<TopListInfo> {
    const result = await axios.get<any>(`/top/list?id=${id}`);
    return result?.playlist;
  }
  /**
   * 获取歌单详情
   */
  async getPlayListDetail(id: CommonIdType): Promise<PlayListDetailResponse> {
    const result = await axios.get<any>(`/playlist/detail?id=${id}`);
    return result;
  }
  /**
   * 获取推荐歌曲（需登录）
   */
  async getRecommendSongs(): Promise<RecommendSongsResponse> {
    const result = await axios.get<any>('/recommend/songs');
    return result;
  }
  /**
   * 获取歌词
   */
  async getLyric(id: CommonIdType): Promise<LyricResponse> {
    const result = await axios.get<any>(`/lyric?id=${id}`);
    return result;
  }
  /**
   * 获取热门MV
   */
  async getTopMV(offset: number, limit = 10): Promise<HotMVResponse> {
    const result = await axios.get<any>(
      `/top/mv?limit=${limit}&offset=${offset}`
    );
    return result;
  }
  /**
   * 获取MV详情
   */
  async getMVDetail(id: CommonIdType): Promise<MVDetailResponse> {
    const result = await axios.get<any>(`/mv/detail?mvid=${id}`);
    return result;
  }
  /**
   * 获取最近MV
   */
  async getMVFirst(offset: number, limit = 10): Promise<HotMVResponse> {
    const result = await axios.get<any>(
      `/mv/first?limit=${limit}&offset=${offset}`
    );
    return result;
  }
  /**
   * 获取相似MV
   */
  async getSimilarMV(id: CommonIdType): Promise<ArtistMVResponse> {
    const result = await axios.get<any>(`/simi/mv?mvid=${id}`);
    return result;
  }
  /**
   * 搜索不同类型的信息
   */
  async searchByType<T extends keyof SearchTypeMap>(
    keywords: string | string[],
    type: SearchType,
    offset = 0,
    limit = 15
  ): Promise<SearchResponseType<T>> {
    keywords = _.isArray(keywords) ? keywords.join(',') : keywords;
    const result = await axios.get<any>(
      `/search?keywords=${keywords}&type=${type}&limit=${limit}&offset=${offset}`
    );
    return result;
  }
  /**
   * 获取一个建议搜索的字符串
   */
  async getSearchDefault(): Promise<SearchDefaultResponse> {
    const result = await axios.get<any>('/search/default');
    return result;
  }
  /**
   * 获取热搜关键字
   */
  async getSearchHot(): Promise<SearchHotResponse> {
    const result = await axios.get<any>('/search/hot');
    return result;
  }
  /**
   * 获取用户歌单
   */
  async getUserPlayList(id: CommonIdType): Promise<PlayListResponse> {
    const result = await axios.get<any>(`/user/playlist?uid=${id}`);
    return result;
  }
  /**
   * 点赞，需要登录
   */
  async setLike(
    id: CommonIdType,
    commentId: CommonIdType,
    likeType: LikeType,
    objectType: CommentObjectType
  ): Promise<void> {
    await axios.get<any>(
      `/comment/like?id=${id}&cid=${commentId}&t=${likeType}&type=${objectType}`
    );
    return;
  }
  /**
   * 往歌单中添加或删除歌曲
   */
  async operatePlayList(
    operation: 'add' | 'del',
    playListId: CommonIdType,
    tracks: CommonIdType | CommonIdType[]
  ): Promise<void> {
    tracks = _.isArray(tracks) ? tracks?.join(',') : tracks;
    await axios.get(
      `/playlist/tracks?op=${operation}&pid=${playListId}&tracks=${tracks}`
    );
    return;
  }
  /**
   * 发送验证码
   */
  async setVerifyCode(phone: number | string): Promise<void> {
    await axios.get(`/captcha/sent?phone=${phone}`);
  }
  /**
   * 使用手机号登录
   */
  async loginByPassword(
    phone: number | string,
    password: string
  ): Promise<LoginResponse> {
    const result = await axios.post<any>(`/login/cellphone`, {
      phone,
      password,
    });
    return result;
  }
  /**
   * 使用手机号登录
   */
  async register(
    phone: number | string,
    password: string,
    captcha: number | string,
    nickname: string
  ): Promise<LoginResponse> {
    const result = await axios.post<any>(`/register/cellphone`, {
      phone,
      password,
      captcha,
      nickname,
    });
    return result;
  }
  /**
   * 获取登录状态
   */
  async loginStatus(): Promise<LoginResponse> {
    const result = await axios.get<any>(`/login/status`);
    return result?.data;
  }
  /**
   * 获取登录状态
   */
  async getQRKey(): Promise<QRKeyResponse> {
    const result = await axios.get<any>(`/login/qr/key?t=${new Date().getTime()}`);
    return result;
  }
  /**
   * 获取登录状态
   */
  async createQRCode(key: string, qrimg: boolean = true): Promise<QRCreateResponse> {
    const result = await axios.get<any>(`/login/qr/create?key=${key}&qrimg=${qrimg}&t=${new Date().getTime()}`);
    return result;
  }
  /**
   * 获取登录状态
   */
  async checkQrCode(key: string): Promise<QRCheckResponse> {
    const result = await axios.get<any>(`/login/qr/check?key=${key}&t=${new Date().getTime()}`);
    return result;
  }
  /**
   * 退出登陆
   */
  async logout(): Promise<Message> {
    const result = await axios.get<any>(`/logout`);
    return result;
  }
  /**
   * 获取mv的url
   */
  async getMVUrl(id: CommonIdType): Promise<MVUrlResponse> {
    const result = await axios.get<any>(`/mv/url?id=${id}`);
    return result;
  }
}

export enum LikeType {
  UnLike = 0,
  Like = 1,
}

export enum CommentObjectType {
  /**
   * 0: 歌曲
   */
  Music = 0,
  /**
   * 1: mv
   */
  MV = 1,
  /**
   * 2: 歌单
   */
  PlayList = 2,
  /**
   * 3: 专辑
   */
  Album = 3,
  /**
   * 4: 电台
   */
  Radio = 4,
  /**
   * 5: 视频
   */
  Video = 5,
  /**
   * @deprecated 暂不支持
   * 6: 动态
   */
  Message = 6,
}

export default new APIType();
